<template>
	<view class="wrap" >
	  <view class="back" @tap="back">
		<image
		:src="$imgUrl('/lt_back.png')" 
		  mode="scaleToFill"
		/>
	  </view>
	  <view class="content">
		<scroll-view scroll-y style="height: 75vh;">
		  <view class="text-dashed">
			<view class="line"></view>
			<view class="view">若退出本页面将不保存历史记录</view>
		  </view>
		  <view class="list" v-for="(item, index) in list" :key="index">
			<view class="list_left list_item" v-if="item.type == 1">
			  <image class="user" src="../../static/logo.jpg"></image>
			  <view class="list_item_information"  v-if="item.img" >
				  <image class="back_user"  mode="widthFix"  :src="item.img" @click="previewImage(item.img)"></image>
			  </view>
			  <view class="list_item_information"  v-else>
				<text>{{ item.text }}</text>
			  </view>
			</view>
			<view class="list_right list_item"  v-else>
			  <view class="list_item_information">
				<image class="back_image"  mode="widthFix"  :src="item.img" @click="previewImage(item.img)"></image>
			  </view>
			  
			  <image class="user" :src="$imgUrl('/user.png')"></image>
			  
			</view>
		  </view>
		</scroll-view>
		<view class="footer">
			  <view class="up" @click="openPhoto">上传图片</view>
			  <view class="start" @click="submit">开始作画</view>
		  </view>
		  
	  </view>
  </view>
  
  
  </template>
  
  <script>
  export default {
	  data() {
		  return {
			  text: '',
			  list: [
				  //type 1 左 2右
				  { type: 1, img: '', text: '请上传图片,本次将消耗20积分' }
			  ],
			  base64: ''
		  };
	  },
	  methods: {
		  /**
		   * 获取动漫画
		   */
		  async submit() {
			  if (this.base64) {
				  this.showLoading('生成中...');
				  let res = await this.$http.post('/app/ai/txxf', { base64: this.base64 });
				  if (res.tp.base64) {
					  this.base64 = '';
					  this.list.push({ type: 1, img: 'data:image/png;base64,' + res.tp.base64 });
				  }
				  console.log(res);
				  this.hideLoading();
			  } else {
				  this.toast('请选择图片');
			  }
		  },
		  /**
		   * 拍照
		   */
		  openPhoto() {
			  uni.chooseImage({
				  count: 1, // 默认9, 设置图片的选择数量
				  sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
				  sourceType: ['album', 'camera'], // 从相册选择
				  success: (res) => {
					  let path = res.tempFilePaths[0];
					  uni.getFileSystemManager().readFile({
						  filePath: path, // 图片文件路径
						  encoding: 'base64', // 指定编码格式为base64
						  success: (res) => {
							  this.base64 = res.data;
							  this.list.push({ type: 2, img: path });
						  },
						  fail: (err) => {
							  console.error('读取文件失败:', err);
						  }
					  });
				  },
				  fail: (err) => {
					  // 处理错误情况
					  console.error(err);
				  }
			  });
		  },
		  /**
		   * 图片查看
		   */
		  previewImage(url) {
			  uni.previewImage({
				  current: 0, // 当前显示图片的索引值
				  urls: [url], // 需要预览的图片列表，photoList要求必须是数组
				  loop: true // 是否可循环预览
			  });
		  },
		  back(){
			uni.navigateBack()
		}
	  }
  };
  </script>
  
  <style lang="scss" scoped>
  @import './css/public.scss';

  </style>
  